package com.example.myonlinemusic.controller;


import com.example.myonlinemusic.mapper.LoveMusicMapper;
import com.example.myonlinemusic.model.Music;
import com.example.myonlinemusic.model.User;
import com.example.myonlinemusic.tools.Constant;
import com.example.myonlinemusic.tools.ResponseBodyMessage;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    //先注入Mapper对象
    @Resource
    private LoveMusicMapper loveMusicMapper;
    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        //从session中获得userId
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            log.info("您当前没有登录");

            return new ResponseBodyMessage<>(-1,"请登录后再进行收藏",false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        //查询是否收藏过音乐
        Music music = loveMusicMapper.findLoveMusic(userId,musicId);
        System.out.println(music);
        if(music != null){
            //收藏过，就不可以再进行收藏了
            return new ResponseBodyMessage<>(-1,"您之前收藏过此音乐，不可重复收藏哦",false);
            //其实这里可以处理成再点一次就取消收藏，但是这里就不这么处理了，在后面收藏页面再进行取消收藏操作
        }
        //表示没有收藏过音乐，可以进行收藏
        boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        //看音乐是否收藏成功
        if(effect){
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"收藏音乐失败",false);
        }

    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String title,HttpServletRequest request){
        List<Music> musicList = null;
        //从session中获得userId
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            log.info("您当前没有登录");

            return new ResponseBodyMessage<>(-1,"请登录后再进行查询",musicList);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        if(title == null){
            //全部查询通过用户id
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);

        }else{
            //有标题，就通过标题和id'进行查询
            musicList = loveMusicMapper.findLoveMusicByTitleAndUserId(userId,title);
        }

        return new ResponseBodyMessage<>(0,"查询到了所有收藏的歌曲",musicList);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        HttpSession httpSession = request.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            log.info("您当前没有登录");

            return new ResponseBodyMessage<>(-1,"请登录后再进行取消",false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(ret == 1){
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }else{
            return new ResponseBodyMessage<>(-1,"取消收藏失败",false);

        }
    }
}
